package com.mz.ozy.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.baidu.mapapi.SDKInitializer;
import com.mz.ozy.R;
import com.mz.ozy.adapter.LogisticsInfoAdapter;
import com.mz.ozy.adapter.MyAdapter;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.OrderLogisticsInfoBean;
import com.mz.ozy.mvp.presenter.OrderLogisticsInfoPresenter;
import com.mz.ozy.mvp.view.OrderLogisticsInfoContract;
import com.mz.ozy.utils.ToastUtils;

import java.util.Collections;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/11/23.
 * 物流信息
 */

public class OrderLogisticsInfoActivity extends BaseActivity<OrderLogisticsInfoContract.View, OrderLogisticsInfoPresenter> implements OrderLogisticsInfoContract.View {


    @BindView(R.id.tv_address)
    TextView mTvAddress;
    @BindView(R.id.tv_courier_number)
    TextView mTvCourierNumber;
    @BindView(R.id.rv_logistics)
    RecyclerView mRvLogistics;
    @BindView(R.id.rv_goods_list)
    RecyclerView mRvGoodsList;
    @BindView(R.id.tv_no_logistics)
    TextView mTvNoLogistics;
    @BindView(R.id.tv_order_seq)
    TextView mTvOrderSeq;
    @BindView(R.id.tv_order_state)
    TextView mTvOrderState;
    @BindView(R.id.webView)
    WebView mWebView;

    private String mOrder;
    private LogisticsInfoAdapter mAdapter;
    private MyAdapter myAdapter;
    private int beanType;
    private String url;
    private String code;
    private String number;
    private int state;
    private String address;

    public static void start(Context context, String order, int beanType) {
        Intent starter = new Intent(context, OrderLogisticsInfoActivity.class);
        starter.putExtra("order", order);
        starter.putExtra("beanType", beanType);
        context.startActivity(starter);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_order_logistics_info);
        ButterKnife.bind(this);
        initTitle();
        code = getIntent().getStringExtra("code");
        number = getIntent().getStringExtra("number");
        mOrder = getIntent().getStringExtra("order");
        beanType = getIntent().getIntExtra("beanType", 0);
        state =  getIntent().getIntExtra("state", 0);
        address = getIntent().getStringExtra("address");
        if(!TextUtils.isEmpty(number)) {
            if (TextUtils.isEmpty(mOrder)) {
                ToastUtils.showToast("数据有误");
                finish();
                return;
            }
            init();
        }
    }

    private void init() {
        tvCenterTitle.setText("物流信息");

        mTvNoLogistics.setVisibility(View.GONE);

        mTvOrderSeq.setText("订单号: " + mOrder);
        mRvLogistics.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new LogisticsInfoAdapter(null);
        mRvLogistics.setAdapter(mAdapter);
        mRvLogistics.setNestedScrollingEnabled(false);
        if(TextUtils.isEmpty(number) || TextUtils.isEmpty(address)) {
            mPresenter.initData(this, mOrder, beanType);
        }else
        {
            mTvAddress.setText(address);
            setOrderState(state);
            mTvCourierNumber.setText(number);
            mPresenter.getLogistics(this, code, number);
        }
    }

    @Override
    public void onInitSuccess(OrderLogisticsInfoBean infoBean) {

        mTvAddress.setText(infoBean.getPledgeMap().getReceiver_province()
                + infoBean.getPledgeMap().getReceiver_city()
                + infoBean.getPledgeMap().getReceiver_region()
                + infoBean.getPledgeMap().getReceiver_detail_address());
//        if (TextUtils.isEmpty(infoBean.getPledgeMap().getCompany()) || TextUtils.isEmpty(infoBean.getPledgeMap().getNumber())) {
//            mTvCourierNumber.setText("");
//            setOrderState(infoBean.getPledgeMap().getState());
//
//            final List<OrderLogisticsInfoBean.LogisticsDetailsBean> traces = infoBean.getTraces();
//            if (traces == null || traces.size() == 0) {
//                mTvNoLogistics.setVisibility(View.VISIBLE);
//                return;
//            }
//
//            Collections.reverse(traces);
//            mTvNoLogistics.setVisibility(View.GONE);
//            //mAdapter.setNewData(traces);
//        } else {
            setOrderState(state);
            mTvCourierNumber.setText(number);
            mPresenter.getLogistics(this, code, number);
//        }
    }
    @Override
    public void getLogisticsSuccess(OrderLogisticsInfoBean infoBean) {

        //获取物流地图
        url = infoBean.getLogistics().getTrailUrl();
        //返回的地图链接加载到webview
        mWebView.loadUrl(url);
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                mWebView.loadUrl(url);
                return super.shouldOverrideUrlLoading(view, url);
            }
        });
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webSettings.setUseWideViewPort(true);
        webSettings.setLoadWithOverviewMode(true);
        webSettings.setSupportZoom(true);
        webSettings.setBuiltInZoomControls(true);
        webSettings.setDisplayZoomControls(false);

        final List<OrderLogisticsInfoBean.logisticsBean.logisticsDataList> traces = infoBean.getLogistics().getLogisticsData();
        if (traces == null || traces.size() == 0) {
            mTvNoLogistics.setVisibility(View.VISIBLE);
            return;
        }

//        Collections.reverse(traces);
        mTvNoLogistics.setVisibility(View.GONE);
        mAdapter.setNewData(traces);
    }

    private void setOrderState(int state) {
        switch (state) {
            case 0://订单生成
                mTvOrderState.setText("待付款");
                break;
            case 1: //已发货
                mTvOrderState.setText("待发货");
                break;
            case 2://已收货
                mTvOrderState.setText("待收货");
                break;
            case 3://退货中
                mTvOrderState.setText("待退款");
                break;
            case 4://已退货
                mTvOrderState.setText("已退款");
                break;
            case 5://已取消
                mTvOrderState.setText("已完成");
                break;
            case 6://未发货
                mTvOrderState.setText("已取消");
                break;
            case 7://订单超时
                mTvOrderState.setText("退款失败");
                break;
        }
    }
}
